var Emitter = require('emitter');

function Animation() {
  Emitter.call(this);
  this.isAnimating = false;
  this.startTime = 0;
  this.totalTime = 500;
}

Animation.prototype = new Emitter();

Animation.prototype.animate = function() {
  //avoid duplicate animation
  if (this.isAnimating) {
    return;
  }
  this.isAnimating = true;
  this.startTime = new Date().getTime();
  if (NotificationReceiver) {
    NotificationReceiver.emit('start-render', this.totalTime);
  }
};

//rewrite step in children
Animation.prototype.step = function() {
  this.value -= 5;
  this.emit('step');
  if (this.value <= 0) {
    this.stop();
  }
};

Animation.prototype.stop = function() {
  if (!this.isAnimating) {
    return;
  }
  this.isAnimating = false;
  this.startTime = 0;
  this.emit('end');
};

Animation.prototype.abort = function() {
  if (!this.isAnimating) {
    return;
  }
  this.isAnimating = false;
  this.startTime = 0;
};

Animation.prototype.dispose = function() {
  this.stop();
};

module.exports = Animation;